$(function(){
    fetchKind();
    if(kind_id.trim() !== ""){
        console.log(kind_id);
        $("#comboList").children("[name="+kind_id+"]").prop("selected",true);
    }
    $("#update-btn").click(function (){
        edit();
    })


    $("#comboList").change(function (){
        $(":input[name = kind_id]").val($(this).val());
    });
    $(":input[name = kind_id]").blur(function (){
        let text = $(":input[name = kind_id]").val();
        let $option =   $("#comboList").children("[name="+text+"]")
        if($option.length == 0){
            layer.msg("该分类编号不存在");
            return;
        }
        $option.prop("selected",true);
    });



});
function edit(){

    let empId = id;
    let food_id = $(":input[name=food_id]").val();
    if(food_id.trim() === ""){
        layer.msg("顾客编号不能为空");
        return false;
    }
    let kind_id = $(":input[name=kind_id]").val();
    if(kind_id.trim() === ""){
        layer.msg("顾客编号不能为空");
        return false;
    }

    let name = $(':input[name=name]').val();
    if(name.trim() === ""){
        layer.msg("顾客姓名编号不能为空");
        return false;
    }
    let price = $(':input[name=price]').val();
    if(price.trim() === ""){
        layer.msg("顾客姓名编号不能为空");
        return false;
    }
    let text = $(":input[name = kind_id]").val();
    let $option =   $("#comboList").children("[name="+text+"]")
    if($option.length == 0){
        layer.msg("该分类编号不存在");
        return;
    }
    let description = $(":input[name=description]").val();
    let url = ctx + "/food/edit";
    console.log(kind_id);
    $.post(url,{
        empId,
        kind_id,
        food_id,
        name,
        price,
        description
    },function (resp){
        if(resp.success){
            layer.alert("修改成功",function (){
                location.href = ctx+"/food/list";
            });
        }else{
            layer.alert(resp.error||"删除失败，请稍后再试");
        }

    },"json");

}
function fetchKind(){
    // layer.msg("触发");

    let url = ctx + "/kind/comboList";
    $.post(url,{},function(resp){
        let klist = resp.klist;
        $("#comboList").empty();
        klist.forEach(function (item){
            if(kind_id == item.kindId){
                $("#comboList").append($("<option value='"+item.kindId+"' name='"+item.kindId+"'"+" selected = `selected`>"+item.name+"</option>"));
            }
            else {
                $("#comboList").append($("<option value='"+item.kindId+"' name='"+item.kindId+"'>"+item.name+"</option>"));
            }

        });
    });

}